package com.zhoug.zmui3demo.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhoug.common3.beans.CharSequenceNameValue;
import com.zhoug.common3.interfaces.NameTransformer;
import com.zhoug.common3.interfaces.OnExpandChangeListener;
import com.zhoug.common3.utils.ViewUtils;
import com.zhoug.zmui3.ZMUIExpandView;
import com.zhoug.zmui3.input.ZMUIInputCheckView;
import com.zhoug.zmui3.list.OnSelectChangeListener;
import com.zhoug.zmui3.list.ZMUISelectionListDialog;
import com.zhoug.zmui3demo.R;

import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @Author: zhoug
 * @Date: 2024-07-16
 * @Description:
 */
public class ZMUIExpandViewActivity  extends AppCompatActivity {
    private ZMUIExpandView expandView11;
    private ZMUIExpandView expandView13;
    private ZMUIExpandView expandView12;
    private ZMUIInputCheckView checkView;
    private ZMUIExpandView expandViewArea;
    private ZMUIExpandView expandViewFirmType;
    private ZMUIExpandView expandViewGrade;
    private ZMUIExpandView expandViewState;
    private TextView tvName;
    private ImageView ivIcon;
    private LinearLayout root;


    protected ViewGroup getRoot(){
        return root;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expand_view);
        findViews();
//        setContentView(getRoot());
        init();
    }

    private void init(){
        expandView11.setOnExpandChangeListener(new OnExpandChangeListener<ZMUIExpandView>() {
            @Override
            public void onExpand(ZMUIExpandView view, boolean expand) {

            }
        });

        ViewUtils.findView(getRoot(), ZMUIExpandView.class, view->{


        });

        initDialog(expandView11, Arrays.asList("北京市","河北省","内蒙古自治区","广西壮族自治区","渝"));
        initDialog(expandView13,  Arrays.asList("北京市","河北省","内蒙古自治区","广西壮族自治区","渝"));
        initDialog(expandViewArea, Arrays.asList("北京市","河北省","内蒙古自治区","广西壮族自治区","渝"));
        initDialog(expandViewGrade, Arrays.asList("A级","B级","C级","D级","暂未评级"));
        initDialog(expandViewState, Arrays.asList("在线","离线","没有摄像头"));
        initDialog(expandViewFirmType, Arrays.asList("商超","批发市场","食品流通企业"));


        checkView.setData(Arrays.asList(new CharSequenceNameValue("左"),
                new CharSequenceNameValue("中"),
                new CharSequenceNameValue("右")));
        checkView.setSelect(checkView.getData().get(1));
        checkView.setCancelSelectEnabled(false);
        checkView.setOnSelectChangeListener(new OnSelectChangeListener<NameTransformer>() {
            @Override
            public void onSelectChange(@NonNull List<NameTransformer> select) {
                CharSequenceNameValue nameTransformer = (CharSequenceNameValue) select.get(0);
                String name = nameTransformer.getData().toString();
                if("左".equals(name)){
                    expandViewArea.setGravity(Gravity.START);
                    expandViewGrade.setGravity(Gravity.START);
                    expandViewState.setGravity(Gravity.START);
                    expandViewFirmType.setGravity(Gravity.START);
                }else if("右".equals(name)){
                    expandViewArea.setGravity(Gravity.END);
                    expandViewGrade.setGravity(Gravity.END);
                    expandViewState.setGravity(Gravity.END);
                    expandViewFirmType.setGravity(Gravity.END);
                }else{
                    expandViewArea.setGravity(Gravity.CENTER);
                    expandViewGrade.setGravity(Gravity.CENTER);
                    expandViewState.setGravity(Gravity.CENTER);
                    expandViewFirmType.setGravity(Gravity.CENTER);
                }
            }

            @Override
            public void onClearSelect() {

            }
        });

    }


    private ZMUISelectionListDialog<String> initDialog(ZMUIExpandView expandView, List<String> data){
        ZMUISelectionListDialog<String> dialog=new ZMUISelectionListDialog<String>(this) {

            @Override
            public String getItemName(String itemData) {
                return itemData;
            }
        };
        dialog.setMultiSelect(false);
        dialog.setData(data);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                expandView.setExpanded(false);
            }
        });
        dialog.setOnSelectChangeListener(new OnSelectChangeListener<String>() {
            @Override
            public void onSelectChange(@NonNull List<String> select) {
                String s = select.get(0);
                expandView.setName(s);
                tvName.setText(s);
                ivIcon.setImageResource(R.drawable.zmui_icon_expand_normal);
            }

            @Override
            public void onClearSelect() {
                expandView.setDefName();
                tvName.setText("测试");
                ivIcon.setImageResource(R.drawable.zmui_icon_expand_normal);

            }
        });
        expandView.setOnExpandChangeListener(new OnExpandChangeListener<ZMUIExpandView>() {
            @Override
            public void onExpand(ZMUIExpandView view, boolean expand) {
                if(expand){
                    dialog.show();
                    ivIcon.setImageResource(R.drawable.zmui_icon_expand_expand);
                }
            }
        });
        return dialog;
    }

    private void findViews() {
        expandView11 = findViewById(R.id.expandView11);
        expandView13 = findViewById(R.id.expandView13);
        expandView12 = findViewById(R.id.expandView12);
        checkView = findViewById(R.id.checkView);
        expandViewArea = findViewById(R.id.expandViewArea);
        expandViewFirmType = findViewById(R.id.expandViewFirmType);
        expandViewGrade = findViewById(R.id.expandViewGrade);
        expandViewState = findViewById(R.id.expandViewState);
        tvName = findViewById(R.id.tv_name);
        ivIcon = findViewById(R.id.iv_icon);
        root = findViewById(R.id.root);
    }
}
